"""
Case Type   : Compatibility
Case Name   : rename table 异常场景校验
Create At   : 2023/02/25
Owner       : @peilinqian
Description :
    step1:创建兼容B库;
    step2:兼容B库库下创建2张表;
    step3:rename table的表不存在;
    step4:rename table后的表名已存在;
    step5:rename table后的表名前后相同;
Expect      :
    step1:创建兼容B库; expect:创建成功
    step2:兼容B库库下创建2张表; expect:创建成功
    step3:rename table的表不存在; expect:失败
    step4:rename table后的表名已存在; expect:失败
    step5:rename table后的表名前后相同; expect:失败
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ClientConnection(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"----{os.path.basename(__file__)} start----")
        self.common = Common()
        self.constant = Constant()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_b_name = 'db_renametable_0002'
        self.t_name1 = 't_renametable_0002_1'
        self.t_name2 = 't_renametable_0002_2'
        self.t_name3 = 't_renametable_0002_3'
        self.t_name4 = 't_renametable_0002_4'
        self.err_flag1 = f'{self.t_name3}" does not exist, skipping'
        self.err_flag2 = f'{self.t_name2}" already exists'
        self.err_flag3 = f'{self.t_name1}" already exists'

    def test_set_password(self):
        step_txt = '----step1:创建兼容B库; expect:创建成功----'
        self.log.info(step_txt)
        create_sql = f"drop database if exists {self.db_b_name};" \
            f"create database {self.db_b_name} dbcompatibility='B';"
        create_result = self.sh_primary.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_result,
                      '执行失败' + step_txt)

        step_txt = '----step2:兼容B库库下创建2张表; expect:创建成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name1};" \
            f"drop table if exists {self.t_name2};" \
            f"create table {self.t_name1}(a int,b int);" \
            f"create table {self.t_name2}(a int,b int);"
        create_result = self.sh_primary.execut_db_sql(create_sql,
                                                      dbname=self.db_b_name)
        self.log.info(create_result)
        self.assertEqual(create_result.count(
            self.constant.CREATE_TABLE_SUCCESS), 2, '执行失败' + step_txt)

        step_txt = '----step3:rename table的表不存在; expect:失败----'
        self.log.info(step_txt)
        rename_sql = f"rename table {self.t_name3} to {self.t_name4};"
        rename_result = self.sh_primary.execut_db_sql(rename_sql,
                                                      dbname=self.db_b_name)
        self.log.info(rename_result)
        self.assertIn(self.err_flag1, rename_result, '执行失败' + step_txt)

        step_txt = '----step4:rename table后的表名已存在; expect:失败----'
        self.log.info(step_txt)
        rename_sql = f"rename table {self.t_name1} to {self.t_name2};"
        rename_result = self.sh_primary.execut_db_sql(rename_sql,
                                                      dbname=self.db_b_name)
        self.log.info(rename_result)
        self.assertIn(self.err_flag2, rename_result, '执行失败' + step_txt)

        step_txt = '----step5:rename table后的表名前后相同; expect:失败----'
        self.log.info(step_txt)
        rename_sql = f"rename table {self.t_name1} to {self.t_name1};"
        rename_result = self.sh_primary.execut_db_sql(rename_sql,
                                                      dbname=self.db_b_name)
        self.log.info(rename_result)
        self.assertIn(self.err_flag3, rename_result, '执行失败' + step_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step_txt1 = '----删除普通库及B库表; expect:删除成功----'
        self.log.info(step_txt1)
        drop_sql = f"drop table if exists {self.t_name1};" \
            f"drop table if exists {self.t_name2};"
        drop_result1 = self.sh_primary.execut_db_sql(drop_sql,
                                                     dbname=self.db_b_name)
        self.log.info(drop_result1)

        step_txt2 = '----删除普通库及B库; expect:删除成功----'
        self.log.info(step_txt2)
        drop_sql = f"drop database if exists {self.db_b_name};"
        drop_result2 = self.sh_primary.execut_db_sql(drop_sql)
        self.log.info(drop_result2)

        self.log.info('----清理环境断言----')
        self.assertEqual(drop_result1.count(
            self.constant.DROP_TABLE_SUCCESS), 2, '执行失败' + step_txt1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_result2,
                      '执行失败' + step_txt2)

        self.log.info(f'----{os.path.basename(__file__)} end----')
